package Or

import (
	"context"
	"fmt"
	"db2s/ETL"
	"db2s/global"
	"db2s/go-log/log"
	"db2s/parDef"
	"db2s/ref"
)

func (cm ColumnMe) Comment(ctx context.Context) (result global.Return, err error) {
	var event = "[Comment]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnCommentSysObjectView, getColumnCommentSql, QueryColValManage)); err != nil {
			err = ref.ErrAddPrintf(event, err)
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (cm ColumnMe) LobMeta(ctx context.Context) (result global.Return, err error) {
	var event = "[LobMeta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getLobColumnSql, QueryColValManage))
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (cm ColumnMe) BigVarcharMeta(ctx context.Context) (result global.Return, err error) {
	var event = "[BigVarcharMeta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getBigColumnSql, QueryColValManage))
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func specialColumnDataProcessing(s any, commentRes global.Return) (res []parDef.ColumnMeta, err error) {
	if s == nil {
		return
	}
	var columnComment = make(map[string]any)
	switch commentRes.Result.(type) {
	case []map[string]any:
		if len(commentRes.Result.([]map[string]any)) > 0 {
			for _, v := range commentRes.Result.([]map[string]any) {
				columnComment[fmt.Sprintf("%v", v["columnName"])] = v["columnComment"]
			}
			//columnComment = commentRes.Result.([]map[string]any)[0]
		}
	}
	var mapValueNull []map[string]any
	for _, v := range s.([]map[string]interface{}) {
		var vv = mapNullActive(v)
		if column, ok := vv["columnName"]; ok && commentRes.Result != nil {
			if val, ok1 := columnComment[fmt.Sprintf("%v", column)]; ok1 {
				vv["columnComment"] = val
			}
		}
		for _, length := range []string{"dataLength", "charLength", "dataPrecision", "dataScale"} {
			if charLength, ok := vv[length]; ok {
				var carl int64
				if carl, err = stringAnyToInt64(charLength); err != nil {
					err = ref.ErrAddPrintf("Oracle.Meta", err)
					return
				}
				vv[length] = carl
			}
		}
		vv["columnType"] = ExtractTypeName(fmt.Sprintf("%v", vv["dataType"]))
		mapValueNull = append(mapValueNull, vv)
		var dd = parDef.ColumnMeta{}
		if err = mapStringToStruct(vv, &dd); err != nil {
			err = ref.ErrAddPrintf("Oracle.Meta", err)
			return
		}
		res = append(res, dd)
	}
	return
}
func (cm ColumnMe) Meta(ctx context.Context) (result global.Return, err error) {
	var event = "[Meta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	var commentRes global.Return
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		if commentRes, err = cm.Comment(parDef.SwapCtxKeyValue(ctx, "expect", ETL.SMap)); err != nil {
			err = ref.ErrAddPrintf(event, err)
			return
		}
		if result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getColumnMetaSql, QueryColValManage)); err != nil {
			err = ref.ErrAddPrintf(event, err)
			return
		}
		if result.Result, err = specialColumnDataProcessing(result.Result, commentRes); err != nil {
			err = ref.ErrAddPrintf(event, err)
			log.ErrorLog().Error(event, err)
			return
		}
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
